﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CNKI.TPI.Web.Search.Model;
using KNet.Data.Entity;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Admin.IDAO;
using System.Linq.Expressions;

namespace CNKI.TPI.Web.Admin.DAO
{
    public class SingleDBPublishDAO : BaseDAO, ISingleDBPublishDAO
    {

        /// <summary>
        /// 获取所有数据库列表
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_DATABASE> GetDBList(IList<SearchType> SearchTypeList = null)
        {

            IList<SYS_CMS_DATABASE> result = null;
            try
            {
                if (SearchTypeList != null)
                {
                    if (SearchTypeList.Count == 0)
                    {
                        return new List<SYS_CMS_DATABASE>();
                    }
                    ExpressionCombine<SYS_CMS_DATABASE> exp = new ExpressionCombine<SYS_CMS_DATABASE>();
                    foreach (SearchType item in SearchTypeList)
                    {
                        exp.Or("SearchType", OperatorType.Equal, (int)item);
                    }
                    result = Context.Get<SYS_CMS_DATABASE>(exp.Compile());
                }
                else
                {
                    result = Context.GetAll<SYS_CMS_DATABASE>();
                }
                if (result != null)
                {
                    result = result.OrderBy(c => c.SortSn).ToList();
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
          

            return result;
        }

        /// <summary>
        /// 获取所有数据库列表 分页
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_DATABASE> GetDBListPageSize(Expression<Func<SYS_CMS_DATABASE, bool>> match, int start, int length, out int count,
            Expression<Func<SYS_CMS_DATABASE, bool>> order = null,
            bool loadLazy = false, string tableName = null, string ctxName = null)
        {

            IList<SYS_CMS_DATABASE> result = null;
            count = 0;
            try
            {
                result = Context.Gets<SYS_CMS_DATABASE>(match, start, length, out count, order, loadLazy, tableName, ctxName);

            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }

        /// <summary>
        /// 获得数据库字段
        /// </summary>
        /// <param name="dbID"></param>
        /// <returns></returns>
        public IList<SYS_CMS_DATABASE_FIELD> GetDBField(string dbCode)
        {
            IList<SYS_CMS_DATABASE_FIELD> result = null;
            try
            {
                ExpressionCombine<SYS_CMS_DATABASE_FIELD> exp = new ExpressionCombine<SYS_CMS_DATABASE_FIELD>();
                exp.And("DataBaseCode", OperatorType.Equal, dbCode);
                result = Context.Get<SYS_CMS_DATABASE_FIELD>(exp.Compile());
                foreach (SYS_CMS_DATABASE_FIELD item in result)
                {
                    item.DatabaseCode = StringHelper.RemoveHighlightFlag(item.DatabaseCode);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbCode"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public SYS_CMS_DATABASE_FIELD GetDBField(string dbCode, string fieldName)
        {
            SYS_CMS_DATABASE_FIELD result = null;
            try
            {
                ExpressionCombine<SYS_CMS_DATABASE_FIELD> exp = new ExpressionCombine<SYS_CMS_DATABASE_FIELD>();
                exp.And("DataBaseCode", OperatorType.Equal, dbCode);
                exp.And("Field", OperatorType.Equal, fieldName);
                IList<SYS_CMS_DATABASE_FIELD> content = Context.Get<SYS_CMS_DATABASE_FIELD>(exp.Compile());
                if (content != null && content.Count > 0)
                {
                    result = content[0];
                    result.DatabaseCode = StringHelper.RemoveHighlightFlag(result.DatabaseCode);
                    result.Field = StringHelper.RemoveHighlightFlag(result.Field);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }

        /// <summary>
        /// 获得数据库字段
        /// </summary>
        /// <param name="fieldList"></param>
        /// <returns></returns>
        public IList<SYS_CMS_DATABASE_FIELD> GetDBFieldByID(IList<int> fieldList)
        {
            IList<SYS_CMS_DATABASE_FIELD> result = null;
            try
            {
                if (fieldList.Count() == 0)
                {
                    return new List<SYS_CMS_DATABASE_FIELD>();
                }
                ExpressionCombine<SYS_CMS_DATABASE_FIELD> exp = new ExpressionCombine<SYS_CMS_DATABASE_FIELD>();
                foreach (int item in fieldList)
                {
                    exp.Or("SYSID", OperatorType.Equal, item);
                }
                result = Context.Get<SYS_CMS_DATABASE_FIELD>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }

        /// <summary>
        /// 获得数据库发布模板信息
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_PUBLISHTYPE> GetTemplateList()
        {
            IList<SYS_CMS_PUBLISHTYPE> result = null;
            try
            {
                result = Context.GetAll<SYS_CMS_PUBLISHTYPE>();
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }

        /// <summary>
        /// 更新数据库表 发布字段
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool UpdatetDBPubFlag(string DbID,int flag)
        {
            bool result = false;
            try
            {
                string sql = " update SYS_CMS_DATABASE set DatabasePubFlag="+flag+" where SySID=" + DbID;
                result = Context.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 保存数据库发布字段（SYS_CMS_DATABASE_PUBLISH_FIELD）
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool InsertPublicField(SYS_CMS_DATABASE_PUBLISH_FIELD obj)
        {
            bool result = false;
            try
            {
                result = Context.Insert<SYS_CMS_DATABASE_PUBLISH_FIELD>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 删除数据库发布字段（SYS_CMS_DATABASE_PUBLISH_FIELD）
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool DeletePublicField(Expression<Func<SYS_CMS_DATABASE_PUBLISH_FIELD, bool>> query)
        {
            bool result = false;
            try
            {
                result = Context.Delete<SYS_CMS_DATABASE_PUBLISH_FIELD>(query);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 获取已经设置的字段
        /// </summary>
        /// <param name="dbID"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public IList<SYS_CMS_DATABASE_PUBLISH_FIELD> GetField(string dbID, FieldType type)
        {
            IList<SYS_CMS_DATABASE_PUBLISH_FIELD> result = null;
            try
            {
                ExpressionCombine<SYS_CMS_DATABASE_PUBLISH_FIELD> exp = new ExpressionCombine<SYS_CMS_DATABASE_PUBLISH_FIELD>();
                exp.And("DBID", OperatorType.Equal, dbID);
                exp.And("FieldType", OperatorType.Equal, (int)type);
                result = Context.Get<SYS_CMS_DATABASE_PUBLISH_FIELD>(exp.Compile());
                if (result != null)
                {
                    foreach (SYS_CMS_DATABASE_PUBLISH_FIELD item in result)
                    {
                        item.DbID = StringHelper.RemoveHighlightFlag(item.DbID);
                        item.FieldType = StringHelper.RemoveHighlightFlag(item.FieldType);
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }
      /// <summary>
        /// 更新数据库表 保存发布形式
      /// </summary>
      /// <param name="DbID">数据库ID</param>
      /// <param name="templateID">模板ID</param>
      /// <param name="visibleFlage">是否显示</param>
      /// <returns></returns>
        public bool UpdatetDBTemplate(string DbID, string templateID, string visibleFlage,string dbsort)
        {
            bool result = false;
            try
            {
                string sql = String.Format(" update SYS_CMS_DATABASE set TemplateID={0},VisibleFlag={1},OrderType={2} where SySID={3}", templateID, visibleFlage,dbsort,DbID);
                result = Context.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 保存显示名称
        /// </summary>
        /// <param name="DbID"></param>
        /// <param name="Name"></param>
        /// <param name="DisplayName"></param>
        /// <returns></returns>
        public bool UpdatetDBDisplayName(string DbID, string Name, string DisplayName)
        {
            bool result = false;
            try
            {
                string sql = String.Format(" update SYS_CMS_DATABASE_PUBLISH_FIELD set fielddispname='{0}' where DBID={1} and fieldname='{2}'", DisplayName, DbID, Name);
                result = Context.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 更改数据库显示顺序
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool UpdateDBSort(IList<SYS_CMS_DATABASE> list)
        {
            bool m = true;
            try
            {
                string sql = "";
                foreach (SYS_CMS_DATABASE item in list)
                {
                    sql = " update SYS_CMS_DATABASE set SortSN='" + item.SortSn + "' where SYSID='" + item.SysID + "'";
                    Context.ExecuteNonQuery(sql);
                }
            }
            catch
            {
                m = false;
            }
            return m;

        }
        /// <summary>
        /// 获得已经设置关联的数据库
        /// </summary>
        /// <param name="dbCode">dbcode为空时，获取所有数据</param>
        /// <returns></returns>
        public IList<SYS_CMS_DATABASE_RELATION> GetDbRelation(string dbCode)
        {
            IList<SYS_CMS_DATABASE_RELATION> result = null;
            try
            {
                if (string.IsNullOrEmpty(dbCode))
                {
                    result = Context.GetAll<SYS_CMS_DATABASE_RELATION>();
                }
                else
                {
                    ExpressionCombine<SYS_CMS_DATABASE_RELATION> exp = new ExpressionCombine<SYS_CMS_DATABASE_RELATION>();
                    exp.And("DBCode", OperatorType.Equal, dbCode);
                    result = Context.Get<SYS_CMS_DATABASE_RELATION>(exp.Compile());
                }
                foreach (SYS_CMS_DATABASE_RELATION item in result)
                {
                    item.DBCode = StringHelper.RemoveHighlightFlag(item.DBCode);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }
        /// <summary>
        /// 保存关联数据库
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool InsertRelation(SYS_CMS_DATABASE_RELATION obj)
        {
            bool result = false;
            try
            {
                result = Context.Insert<SYS_CMS_DATABASE_RELATION>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        public bool DeleteRelation(string dbcode)
        {
            bool result = false;
            try
            {
                string sql = " update SYS_CMS_DATABASE_RELATION set status=9 where dbcode=" + dbcode;
                result = Context.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
    }
}
